package com.lagou.service;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.lagou.bean.UpLoadResult;
import com.lagou.config.AliyunConfig;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.UUID;
@Service
public class FileUpLoadService {
    @Autowired
    private AliyunConfig aliyunConfig;
    @Autowired
    private OSSClient  ossClient;
    // 允许上传的格式  .pdf 是测试文件大小时使用
    private static final String[] IMAGE_TYPE = new String[]{ ".jpg",
            ".png",".pdf"};
    public UpLoadResult  upload(MultipartFile multipartFile){
        // 校验图片格式
        boolean  isLegal = false;
        for (String type:IMAGE_TYPE){
            if(StringUtils.endsWithIgnoreCase(multipartFile.getOriginalFilename(),type)){
                isLegal = true;
                break;
            }
        }
        UpLoadResult upLoadResult = new UpLoadResult();
        if (!isLegal){
            upLoadResult.setStatus("error 文件类型不符合 请重新选择");
            return  upLoadResult;
        }
        System.out.println("file_size:"+multipartFile.getSize());
        if(multipartFile.getSize()>1024*1024*5){
            upLoadResult.setStatus("error 文件超过5m 请重新选择");
            return  upLoadResult;
        }
        String fileName = multipartFile.getOriginalFilename();
        String filePath = getFilePath(fileName);
        try {
            ossClient.putObject(aliyunConfig.getBucketName(),filePath,new ByteArrayInputStream(multipartFile.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
            // 上传失败
            upLoadResult.setStatus("error");
            return  upLoadResult;
        }
        upLoadResult.setStatus("done");
        upLoadResult.setName(aliyunConfig.getUrlPrefix()+filePath);
        upLoadResult.setUid(filePath);
        return  upLoadResult;
    }
    // 生成不重复的文件路径和文件名
    private String getFilePath(String sourceFileName) {
        DateTime dateTime = new DateTime();
        return "images/" + dateTime.toString("yyyy")
                + "/" + dateTime.toString("MM") + "/"
                + dateTime.toString("dd") + "/" + UUID.randomUUID().toString() + "." +
                StringUtils.substringAfterLast(sourceFileName, ".");
    }

    public UpLoadResult  deleteFile(String filePath) {
        UpLoadResult upLoadResult = new UpLoadResult();

        try {
            if (!ossClient.doesObjectExist(aliyunConfig.getBucketName(),filePath)){
                upLoadResult.setStatus("删除的文件不存在");
                return  upLoadResult;
            }
            ossClient.deleteObject(aliyunConfig.getBucketName(),filePath);
            upLoadResult.setStatus("删除文件成功");
        } catch (Exception e) {
            e.printStackTrace();
            upLoadResult.setStatus("删除文件失败");
        }
        return  upLoadResult;
    }
}
